package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserLevel
//@description: 创建UserLevel记录
//@param: userLevel model.UserLevel
//@return: err error

func CreateUserLevel(userLevel model.UserLevel) (err error) {
	err = global.GVA_DB.Create(&userLevel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserLevel
//@description: 删除UserLevel记录
//@param: userLevel model.UserLevel
//@return: err error

func DeleteUserLevel(userLevel model.UserLevel) (err error) {
	err = global.GVA_DB.Delete(&userLevel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserLevelByIds
//@description: 批量删除UserLevel记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserLevelByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserLevel{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserLevel
//@description: 更新UserLevel记录
//@param: userLevel *model.UserLevel
//@return: err error

func UpdateUserLevel(userLevel model.UserLevel) (err error) {
	err = global.GVA_DB.Save(&userLevel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserLevel
//@description: 根据id获取UserLevel记录
//@param: id uint
//@return: err error, userLevel model.UserLevel

func GetUserLevel(id uint) (err error, userLevel model.UserLevel) {
	err = global.GVA_DB.Where("id = ?", id).First(&userLevel).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserLevelInfoList
//@description: 分页获取UserLevel记录
//@param: info request.UserLevelSearch
//@return: err error, list interface{}, total int64

func GetUserLevelInfoList(info request.UserLevelSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserLevel{})
    var userLevels []model.UserLevel
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&userLevels).Error
	return err, userLevels, total
}